package co.th.bjc.md.dbonline.control;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import co.th.bjc.md.dbonline.bean.CustomerDBOnlineBean;
import co.th.bjc.md.dbonline.bean.RegionDBOnlineBean;
import co.th.bjc.md.helper.CustomerDataHelper;
import co.th.bjc.md.helper.RegionHelper;
import co.th.bjc.md.model.CustomerDetails;
import co.th.bjc.md.model.Region;

public class CustomerDBOnlineControl {
	
	private static Logger _log = Logger.getLogger(CustomerDBOnlineControl.class);
		
	public List<CustomerDBOnlineBean> searchCustomerAll(){
		List<CustomerDBOnlineBean> listCusBean = new ArrayList<CustomerDBOnlineBean>(0);
		try{
			List<CustomerDetails> listCustomer = new CustomerDataHelper().findAllCustomerDetails();
			CustomerDBOnlineBean bean = null;
			for(CustomerDetails customer : listCustomer){
				bean = copyCustomerDetailsToCustomerBean(customer);
				listCusBean.add(bean);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return listCusBean;
	}
	
	public List<CustomerDBOnlineBean> searchCustomer(CustomerDBOnlineBean bean){
		List<CustomerDBOnlineBean> listCusBean = new ArrayList<CustomerDBOnlineBean>(0);
		try{
			List<CustomerDetails> listCustomer = new CustomerDataHelper().findCustomerDetailsByCriteria(
					bean.getType(), bean.getHospitalDepartment(), 
					bean.getBedCapacityMin(), bean.getBedCapacityMax(), 
					bean.getPresidentImpRate(), bean.getPresidentRpRate(), 
					bean.getDirectorImpRate(), bean.getDirectorRpRate(), 
					bean.getLimitFrom().intValue(), bean.getLimitTo().intValue());
			
			for(CustomerDetails customer : listCustomer){
				bean = copyCustomerDetailsToCustomerBean(customer);
				listCusBean.add(bean);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return listCusBean;
	}
	
	public List<CustomerDBOnlineBean> searchCustomerByRegion(String region){
		List<CustomerDBOnlineBean> listCusBean = new ArrayList<CustomerDBOnlineBean>(0);
		try{
			List<CustomerDetails> listCustomer = new CustomerDataHelper().findCustomerDetailsByRegion(region);
			CustomerDBOnlineBean bean = null;
			for(CustomerDetails customer : listCustomer){
				bean = copyCustomerDetailsToCustomerBean(customer);
				listCusBean.add(bean);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return listCusBean;
	}
	
	public CustomerDBOnlineBean getViewItem(BigInteger customer_id){
		CustomerDBOnlineBean bean = new CustomerDBOnlineBean();
		try{
			CustomerDetails customer = new CustomerDataHelper().findCustomerDetailsByCustomerID(BigDecimal.valueOf(customer_id.longValue()));
			if (customer != null){
				bean = copyCustomerDetailsToCustomerBean(customer);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return bean;
	}
	
	private CustomerDBOnlineBean copyCustomerDetailsToCustomerBean(CustomerDetails customer) throws Exception{
		CustomerDBOnlineBean bean = new CustomerDBOnlineBean();
		bean.setCustomerDataId(BigInteger.valueOf(customer.getCustomerDataId().longValue()));
		bean.setCustomerDataCode(customer.getCustomerDataCode());
		
		bean.setHospitalCode(null);
		bean.setHospitalNameTh(customer.getHospitalNameTH());
		bean.setHospitalNameEn(customer.getHospitalNameEN());
		bean.setHospitalDepartmentCode(null);
		bean.setHospitalDepartment(customer.getHospitalDepartment());
		bean.setHospitalAddress(customer.getHospitalAddress());
		bean.setHospitalEmail(customer.getHospitalEmail());
		bean.setHospitalTelephone(customer.getHospitalTelephone());
		bean.setType(customer.getType());
		bean.setBedCapacityMin(customer.getBedCapacityMin().intValue());
		bean.setBedCapacityMax(customer.getBedCapacityMax().intValue());
		
		bean.setRegionCode(null);
		bean.setRegion(customer.getRegion());
		
		bean.setPresidentName(customer.getPresidentName());
		bean.setPresidentTelephone(customer.getPresidentTelephone());
		bean.setPresidentEmail(customer.getPresidentEmail());
		bean.setPresidentImpRate(customer.getPresidentImpRate().intValue());
		bean.setPresidentRpRate(customer.getPresidentRpRate().intValue());
		
		bean.setDirectorName(customer.getDirectorName());
		bean.setDirectorTelephone(customer.getDirectorTelephone());
		bean.setDirectorEmail(customer.getDirectorEmail());
		bean.setDirectorImpRate(customer.getDirectorImpRate().intValue());
		bean.setDirectorRpRate(customer.getDirectorRpRate().intValue());

		bean.setSalePerson(customer.getSalePerson());
		bean.setBjcDepartment(customer.getBjcDepartment());
		bean.setBjcCategory(customer.getBjcCategory());
		bean.setBjcBrand(customer.getBjcBrand());
		bean.setBjcModel(customer.getBjcModel());
		bean.setStCompetitor(customer.getStCompetitor());
		bean.setNdCompetitor(customer.getNdCompetitor());
		bean.setRdCompetitor(customer.getRdCompetitor());
		
		bean.setRemark1(customer.getRemark1());
		bean.setRemark2(customer.getRemark2());
		
		bean.setImg(customer.getImage()!=null?customer.getImage().getBinaryStream():null);
		return bean;
	}
	
	public List<RegionDBOnlineBean> findAllRegion(){
		List<RegionDBOnlineBean> listBean = new ArrayList<RegionDBOnlineBean>(0);
		try{
			List<Region> listRegion = new RegionHelper().findAllRegion();
			if (listRegion != null && !listRegion.isEmpty()){
				for(Region region : listRegion){
					RegionDBOnlineBean bean = new RegionDBOnlineBean();
					bean.setRegionId(region.getRegionId());
					bean.setRegionNameTH(region.getRegionNameTH());
					bean.setRegionNameEN(region.getRegionNameEN());
					
					listBean.add(bean);
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return listBean;
	}

//	public List<HospitalDBOnlineBean> getHospitalList(BigDecimal region_code, String hospitalNameTH, String hospitalNameEN){
//		List<HospitalDBOnlineBean> list = new ArrayList<HospitalDBOnlineBean>(0);
//		try {
//			List<Hospital> listHospital = service.getHospital(region_code, hospitalNameTH, hospitalNameEN);
//			for(Hospital hospital : listHospital){
//				HospitalDBOnlineBean bean = new HospitalDBOnlineBean();
//				bean.setHospitalId(hospital.getHospitalCode());
//				bean.setHospitalCode(hospital.getHospitalCode().toString());
//				bean.setHospitalName(hospital.getHospitalNameTH());
//				bean.setHospitalNameEn(hospital.getHospitalNameEN());
//				list.add(bean);
//			}
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//		return list;
//	}
//	
//	public List<HospitalDepartmentDBOnlineBean> getHospitalDepartmentList(BigDecimal hospital_code){
//		List<HospitalDepartmentDBOnlineBean> list = new ArrayList<HospitalDepartmentDBOnlineBean>(0);
//		HospitalDepartmentDBOnlineBean hd1 = new HospitalDepartmentDBOnlineBean();
//		hd1.setDepartmentId("01");
//		hd1.setHospitalDepartmentName("X-Rey");
//		list.add(hd1);
//		HospitalDepartmentDBOnlineBean hd2 = new HospitalDepartmentDBOnlineBean();
//		hd2.setDepartmentId("02");
//		hd2.setHospitalDepartmentName("OB-GYN");
//		list.add(hd2);
//		return list;
//	}
//	
//	public List<DepartmentDBOnlineBean> getBjcDepartment(){
//		List<DepartmentDBOnlineBean> list = new ArrayList<DepartmentDBOnlineBean>(0);
//		try {
//			List<Department> listDepartment = service.getAllDepartment();
//			for(Department dept : listDepartment){
//				DepartmentDBOnlineBean bean = new DepartmentDBOnlineBean();
//				bean.setDepartmentCode(dept.getDepartmentCode());
//				bean.setDepartmentName(dept.getDepartmentName());
//				list.add(bean);
//			}
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//		return list;
//	}
//	
//	public boolean insertCustomerData(CustomerDataDBOnlineBean cus){
//		CustomerDetails customerDetails = new CustomerDetails();
//		customerDetails.setCustomerDataId(new BigDecimal(4));
//		customerDetails.setBedCapacityMax(BigDecimal.valueOf(cus.getBedCapacityMax()));
//		customerDetails.setBedCapacityMin(BigDecimal.valueOf(cus.getBedCapacityMin()));
//		customerDetails.setHospitalAddress(cus.getHospitalAddress());
//		customerDetails.setHospitalEmail(cus.getHospitalEmail());
//		customerDetails.setHospitalTelephone(cus.getHospitalTelephone());
//		customerDetails.setHospitalDepartment(cus.getHospitalDepartment());
//		customerDetails.setDirectorImpRate(BigDecimal.valueOf(cus.getDirectorImpRate()));
//		customerDetails.setDirectorName(cus.getDirectorName());
//		customerDetails.setDirectorEmail(cus.getDirectorEmail());
//		customerDetails.setDirectorTelephone(cus.getDirectorTelephone());
//		customerDetails.setDirectorRpRate(BigDecimal.valueOf(cus.getDirectorRpRate()));
//		customerDetails.setHospitalNameTH(cus.getHospitalNameTh());
//		customerDetails.setHospitalNameEN(cus.getHospitalNameEn());
//		customerDetails.setPresidentImpRate(BigDecimal.valueOf(cus.getPresidentImpRate()));
//		customerDetails.setPresidentName(cus.getPresidentName());
//		customerDetails.setPresidentEmail(cus.getPresidentEmail());
//		customerDetails.setPresidentTelephone(cus.getPresidentTelephone());
//		customerDetails.setPresidentRpRate(BigDecimal.valueOf(cus.getPresidentRpRate()));
//		customerDetails.setSalePerson(cus.getSalePerson());
//		customerDetails.setType(cus.getType());
//		customerDetails.setBjcBrand(cus.getBjcBrand());
//		customerDetails.setBjcCategory(cus.getBjcCategory());
//		customerDetails.setBjcDepartment(cus.getBjcDepartment());
//		customerDetails.setBjcModel(cus.getBjcModel());
//		customerDetails.setRegion(cus.getRegion());
//		customerDetails.setStCompetitor(cus.getStCompetitor());
//		customerDetails.setNdCompetitor(cus.getNdCompetitor());
//		customerDetails.setRdCompetitor(cus.getRdCompetitor());
//		customerDetails.setRemark1(cus.getRemark1());
//		customerDetails.setRemark2(cus.getRemark2());
//		
//		InputStream stream = cus.getImg();
//		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//		Blob blob = null;
//		try {
//			Streams.copy(stream, bytes, true /* close stream after copy */);
//
//			blob = new SerialBlob(bytes.toByteArray());
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}
//
//		customerDetails.setImage(blob);
//		try {
//			return service.insertCustormerDataDetails(customerDetails);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return false;
//		}
//	}
//	
//	
//	public CustomerDataDBOnlineBean insertCustomerDatials(CustomerDataDBOnlineBean bean){
//		
////		service.save();
//		CustomerDetails model = new CustomerDetails();
//		
//		model.setCustomerDataId(BigDecimal.valueOf(bean.getCustomerDataId().longValue()));
//		
//		
//		
//		return bean;
//	}
//	
//	public InputStream getImageBlob(BigInteger id){
//		try {
////			CustomerDetails customer = service.getCustomerDetailsByCriteria(id);
//			Blob blob = customer.getImage();
//			
//			return blob.getBinaryStream();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//	
//	public CustomerDataDBOnlineBean getEditItem(BigInteger id){
//		return null;
//	}
//	
//	public HospitalDBOnlineBean getHospitalDepartmentDetail(String hospitalCode, String departmentCode){
//		return null;
//	}
//	
//	public List<CategoryDBOnlineBean> getCategory(){
//		List<CategoryDBOnlineBean> listBean = new ArrayList<CategoryDBOnlineBean>(0);
//		try {
//			List<Category> listMode = service.getAllCategory();
//			
//			for(Category model : listMode){
//				CategoryDBOnlineBean bean = new CategoryDBOnlineBean();
//				bean.setCategoryCode(model.getCategoryCode());
//				bean.setCategoryName(model.getCategoryName());
//				listBean.add(bean);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return listBean;
//	}
//	
//	public List<BrandDBOnlineBean> getBrand(BigDecimal categoryCode){
//		List<BrandDBOnlineBean> listBean = new ArrayList<BrandDBOnlineBean>(0);
//		try {
//			List<Brand> listMode = service.getAllBrand();
//			
//			for(Brand model : listMode){
//				BrandDBOnlineBean bean = new BrandDBOnlineBean();
//				bean.setBrandCode(model.getBrandCode());
//				bean.setBrandName(model.getBrandName());
//				listBean.add(bean);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return listBean;
//	}
//	
//	public List<ModelDBOnlineBean> getModel(BigDecimal brandCode){
//		List<ModelDBOnlineBean> listBean = new ArrayList<ModelDBOnlineBean>(0);
//		try {
//			List<Product> listMode = service.getListModel(null, brandCode);
//			
//			for(Product model : listMode){
//				ModelDBOnlineBean bean = new ModelDBOnlineBean();
//				bean.setModelCode(model.getModel().getModelCode());
//				bean.setModelName(model.getModel().getModelName());
//				listBean.add(bean);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return listBean;
//	}
}
